﻿using GKQC.Common;
using GKQC.DbHelper.Dao;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Xml.Linq;
using System.ComponentModel;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Drawing;

namespace GKQC
{
    public class UpdateScriptHelper
    {
        public static void UpdateScript(AllDbContext dbContext)
        {
            List<string> list = GetScript();
            foreach(string s in list)
            {
                try
                {
                    object o = null;
                    dbContext.QcDb.Ado.ExecuteCommand(s, o);
                }
                catch(Exception ex)
                {
                    Logger.LogError(ex);
                }
            }
        }

        static List<string> GetScript()
        {
            List<string> scriptList = new List<string>();

            if (!string.IsNullOrWhiteSpace(QcFormBase.Version) && QcFormBase.Version.StartsWith("11"))
                scriptList.Add(@"Begin
if not exists(select top 1 1 from[t_rep_set] where[Code] = 'R109')
Insert[t_rep_set]([Code],[BillName],[GroupName],[Name],[Count],[ElectricQty],[SizeQty],[IsUse],[Path],[HasLc2],[HasEsr2],[HasZ2],[Remark],[ModiDate],[ModiUser],[SortId])values(
N'R109', N'信賴性試驗檢測單', N'109', N'高低温测试表单', 1, 30, 0, 1, N'实验室\高低温测试表单.xls', 0, 0, 0, Null, '2024-05-26 12:23:33', N'Admin', 1090)

End");


            scriptList.Add(@"if not exists(select 1 from t_sys_params where Code = 'ErpApiUrl')
begin
    insert into t_sys_params(Code, [Name], [Value], IsSys, Remark, ModiDate, ModiUser)

    Values('ErpApiUrl', N'ERP服务器api地址', 'http://192.168.32.99', 1, '', GETDATE(), 'Admin')
end
else
                begin
                   update t_sys_params set[Value] = 'http://192.168.32.99' where Code = 'ErpApiUrl' and isnull([Value],'')= ''
end");

            scriptList.Add(@"if not exists(select 1 from t_sys_params where Code='ErpApiUrl')
begin
    insert into t_sys_params(Code,[Name],[Value], IsSys,Remark,ModiDate,ModiUser)
	Values('ErpApiUrl',N'ERP服务器api地址','http://192.168.32.99', 1,'',GETDATE(),'Admin')
end
else
begin
   update t_sys_params set [Value]='http://192.168.32.99' where  Code='ErpApiUrl' and isnull([Value],'')=''
end ");

            return scriptList;
        }
    }

}
